package com.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bean.ResponseBean;
import com.pojo.Dep;
import com.service.DepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/dep/")
@CrossOrigin("*")
public class DepAction {

    @Autowired
    private DepService depService;

    @RequestMapping("/findAll")
    public List findAll() {
        return depService.list();
    }


    @RequestMapping("/add")
    public String add()
    {
        Dep dep = new Dep();
        dep.setName("国防部");
        depService.add(dep);
        return "success";
    }


    @RequestMapping("/search")
    public ResponseBean search(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        Page  _page = new Page(page,size); //封装分页数据
        IPage  result =  depService.page( _page);
        return new ResponseBean(0,result.getTotal(),result.getRecords());
    }


    @RequestMapping("/prefixDepName")
    public List prefixDepName(String pre) {
        QueryWrapper q = new QueryWrapper();
        q.like("name", pre + "%");
        //q.eq("id",1L);
        List list = depService.list(q);
        return list;
    }
}
